<?php
$id = Zend_Controller_Front::getInstance()->getRequest()->getParam('id');
$peer = BlogMessagePeer::getInstance();
$select = $peer->select()->where('id = ?', $id);
$message = $peer->fetchRow($select);
if (!$message) $message = BlogMessagePeer::getInstance()->createRow();
$form = $this->element;
?>
<link rel="stylesheet" media="screen" href="http://<?php echo Config::getInstance()->default->hostname; ?>/style/admin_product.css" />
<!--[if lte IE 7]><link href="/style/ie.css" rel="stylesheet" type="text/css"><![endif]-->
<form enctype="multipart/form-data" method="post" action="">
  <div>
    <?php
    /**
     * Translate some form errrors.
     *
     * @param string $key
     * @param string $error
     * @return string
     */
    function _translate($key, $error)
    {
    	$_translated = array(
			'isEmpty' => 'Поле обязательно для заполнения',
		);
	
    	if (isset($_translated[$key])) {
    		return $_translated[$key];
    	}
    	
    	return $error;
    }
    $errors = $form->getMessages();
    foreach ($errors as $key => $error) {
    	if (is_array($error)) {
    		unset($errors[$key]);
    	}

   		if (is_array($error)) {
     		foreach ($error as $k => $v) {
     			$label = $form->getElement($key)->getLabel();
     			$v = _translate($k, $v);
     			$errors[$key][$k] = "$label -- $v";
     		}
    	} else {
    		$errors[$key] = _translate($key, $error);
    	}
    }
    
    if ($errors) {
	    echo "<ul class='errors'>";
	    foreach ($errors as $error) {
	    	if (is_array($error)) {
	    		foreach ($error as $k => $v) {
	    			echo "<li>$v</li>";
	    		}
	    	} else {
	    		echo "<li>$error</li>";
	    	}
	    }
	    echo "</ul>";
    }
    ?>
	<?php
		$title = $form->getElement('title');
		echo $title;
	?>
	<br>
    <?php echo $form->getElement('message'); ?>
	<br>
	<?php
		  $datas = $form->getElement('create_at');
		  $datas->setAttrib('readonly', 'readonly');
		  $datas->setAttrib('style', 'background-color: #EEE;');
		  if(!$message['create_at']){
			  $datas->setValue(date('Y-m-d H:i:s'));
		  }
		  echo $datas;
    ?>
	<br>
	<label><b>Перечислите теги через запятую</b></label><br>
	<input style="width:300px;" type="text" id="tag_list" value="">
	<input type="submit" value="Добавить" onclick="blog_tag_add('<?php echo $id; ?>', $('#tag_list').val()); return false;">
	<br>
	<?php
        $is_tag = TagPeer::getInstance();
	    $select = $is_tag->select();
	    $select->setIntegrityCheck(false);
	    $select->join('tag2blog_message','`tag2blog_message`.`tag_id` = `tag`.`id`',array('blog_tag_id'=>'id'));
	    $select->where('tag2blog_message.blog_message_id =?', $id);
	    $result = $is_tag->fetchAll($select);
	    echo "<ul id='tag_list' style='margin-left: 20px;'>";
	    foreach($result as $list)
	    {
			echo "<li id='".$list['blog_tag_id']."' style='color:#8a2be2;'><input style='margin-right: 5px;' onclick='blog_tag_del(".$list['blog_tag_id']."); return false;' type='submit' value='del'>".$list['name']."</li>";
	    }
	    echo "</ul>";
    ?>
	<br>
	<br>
   <dd id="submit-element">
        <input type="submit" name="submit" id="submit" value="Сохранить">
	   <?php echo $form->getElement('crud_exit'); ?>
   </dd>
  </div>
</form>

<script type="text/javascript">
	function convert2EN(from)
  {
	  var rusChars = new Array('а','б','в','г','д','е','ё','ж','з','и','й','к','л','м','н','о','п','р','с','т','у','ф','х','ч','ц','ш','щ','э','ю','\я','ы','ъ','ь', ' ', '\'', '\"', '\#', '\$', '\%', '\&', '\*', '\,', '\:', '\;', '\<', '\>', '\?', '\[', '\]', '\^', '\{', '\}', '\|', '\!', '\@', '\(', '\)', '\-', '\=', '\+', '\/', '\\');
	  var transChars = new Array('a','b','v','g','d','e','jo','zh','z','i','j','k','l','m','n','o','p','r','s','t','u','f','h','ch','c','sh','csh','e','ju','ja','y','', '', ' ', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');

  from = from.toLowerCase();
  var to = "";
  var len = from.length;
  var character, isRus;
  for(var i=0; i < len; i++)
    {
    character = from.charAt(i,1);
    isRus = false;
    for(var j=0; j < rusChars.length; j++)
      {
      if(character == rusChars[j])
        {
        isRus = true;
        break;
        }
      }
    to += (isRus) ? transChars[j] : character;
    }
   return to;
  }

	function blog_tag_del(id){
		var id = id;
		if(id){
			$.post(
			'/admin/blog/deltag/',
			{ id: id},
			function(data) {
				if(data){
					$('ul#tag_list').find('li#'+data).hide();
				}
			},
			'json');
		}
	}


	function blog_tag_add(id, tag_list){
		var id = id;
		var tags = tag_list;
		var arr_tag = tags.split(',');
		var arr_tag_en = new Array();
		if(tags.length > 0){
			for(var i = 0; i <= arr_tag.length; i++){
				if(arr_tag[i] != undefined)
				{
					arr_tag_en[i] = convert2EN(arr_tag[i]);
				}
			}
		}

		if(arr_tag_en.length == 0){
			$('#tag_error').html('* Введите теги.');
		}else{
			$.post(
			'/admin/blog/addtag/',
			{ id: id, tags_rus:arr_tag, tags_eng:arr_tag_en},
			function(data) {
				if(data == 'ok'){
					location.reload();
				}
			},
			'json');
		}
	}
</script>